package com.example.petadopt.controller;

import java.util.*;
import com.example.petadopt.controller.vo.Result;
import com.example.petadopt.dao.PetDao;
import com.example.petadopt.entity.Pet;
import com.example.petadopt.dao.ImgDao;
import com.example.petadopt.entity.Img;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/pet")
public class PetController {
    @Autowired
    private PetDao petDao;
    @Autowired
    private ImgDao imgDao;

    @PostMapping("/add")
    public Result add(@RequestBody Pet pet){
        // 设置宠物的发送时间
        pet.setPetSendtime(java.time.LocalDateTime.now());
        // 设置宠物的状态ID
        pet.setPetStateId(8);
        Pet savedPet = petDao.save(pet);
        return new Result(2000,"添加成功",savedPet);
    }

    // 添加获取宠物列表的接口
    @GetMapping("/list")
    public Result list() {
        System.out.println("PetController.list() called");
        List<Pet> pets = petDao.findAll();
        for (Pet pet : pets) {
            List<String> images = new ArrayList<>();
            List<Img> imgList = imgDao.findByImgPetId(pet.getPetId());
            for (Img img:imgList) {
                images.add(img.getImgUrl());
            }
            pet.setPetImages(images);
        }
        return new Result(2000, "获取宠物列表成功", pets);
    }
}

